package com.niit.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.niit.pojo.Menu;
import com.niit.utils.JDBCUtils;

public class MenuDao {
	/**
	 * 根据角色id查询菜单列表
	 * @param roleId
	 * @return
	 * @throws SQLException
	 */
	public List<Menu> listMenuByRole(List<Integer> roleIds) throws SQLException{
		List<Menu> mList = new ArrayList<>();
		Connection connection = JDBCUtils.getConnection();
		StringBuilder sb = new StringBuilder();
		for (Integer roleId : roleIds) {
			sb.append(roleId+",");
		}
		String sql = "select * from menu m LEFT JOIN role_menu rm on m.menu_id = rm.menu_id where rm.role_id = ?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, sb.toString().substring(0, sb.length()-1));
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			int menuId = rs.getInt("menu_id");
			String menuName = rs.getString("menu_name");
			Menu menu = new Menu(menuId,menuName);
			mList.add(menu);
		}
		return mList;
		
	}

}
